package org.jeecg.modules.jx.controller;

import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.miniAppCustomerEndPoint.entity.JxOrderDo;
import org.jeecg.modules.miniAppCustomerEndPoint.enums.JxOrderStateEnum;
import org.jeecg.modules.miniAppCustomerEndPoint.mapper.JxOrderMapper;
import org.jeecg.modules.miniAppCustomerEndPoint.service.JxOrderServiceImpl;
import org.jeecg.modules.miniAppCustomerEndPoint.service.JxWxPayServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/jx/forDebug")
public class JxDebugController {

    @Autowired
    private JxOrderMapper jxOrderMapper;

    @Autowired
    private JxOrderServiceImpl jxOrderService;

    @Autowired
    private JxWxPayServiceImpl jxWxPayService;

    /**
     * 将时间戳为某个时间的，特定状态的订单，全部更新为 新状态
     * @param dateFlag
     * @param oldState
     * @param newState
     * @return
     */
    @PutMapping("/convertAllOrderState")
    public Result<?> convertAllOrderState(
        @RequestParam("dateFlag") Integer dateFlag, @RequestParam("oldState") String oldState, @RequestParam("newState") String newState) {

        try{
            jxOrderMapper.updateAllOrderToState(dateFlag, oldState, newState);
        }catch (Exception ex) {
            ex.printStackTrace();
            return Result.error(ex.getMessage());
        }

        return Result.ok();
    }

    @PutMapping("/convertOrderState")
    public Result<?> convertOrderState(
            @RequestParam("orderId") String orderId, @RequestParam("state") Integer state) {

        try{
            JxOrderDo jxOrderDo = new JxOrderDo();
            jxOrderDo.setId(orderId);
            JxOrderStateEnum stateEnum = JxOrderStateEnum.ALL;
            switch(state) {
                case 1:
                    stateEnum = JxOrderStateEnum.TOPAY;
                    break;
                case 2:
                    stateEnum = JxOrderStateEnum.TOSEND;
                    break;
                case 3:
                    stateEnum = JxOrderStateEnum.TORECEIVE;
                    break;
                case 4:
                    stateEnum = JxOrderStateEnum.TOREVIEW;
                    break;
            }

            jxOrderDo.setState(stateEnum);
            jxOrderMapper.updateById(jxOrderDo);
        }catch (Exception ex) {
            ex.printStackTrace();
            return Result.error(ex.getMessage());
        }

        return Result.ok();
    }


    @GetMapping("/queryOrderPayDetailById")
    public Result<?> queryOrderPayDetailById(
            @RequestParam("orderId") String orderId) {

        JxOrderDo jxOrderDo = jxOrderMapper.selectById(orderId);
        return Result.ok(jxOrderDo);
    }

    @GetMapping("/queryTradeBillDownloadUrl")
    public Result<?> queryTradeBillDownloadUrl(@RequestParam("billDate") String billDate) {
        jxWxPayService.queryTransactionBill(billDate);
        jxWxPayService.queryFundBill(billDate);
        return Result.ok();
    }

}
